// 引入接口文件
import { queryInfo,showUse,getProjectList,verifyCodeFun} from './taskInfoServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config'

// 初始默认状态
const defultState = {
  loading: false,
  actionType: 'add',
  userList: [],
  areaList: [],
  detail: {
    methodName:'',
    endTime: '',
    startTime: '',
    createrName: '',
    target: '',
    addressName: '',
    taskName: '',
    channel: '',
    projectName: '',
    areaId: ''
  },
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
          dispatch({type: 'verifyCodeFun',payload: {query}})
          
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    // 查询记录
    *queryInfo({payload}, {put, select, call}){
      let {detail} = yield select(state => state[tmpModule.namespace])
      let params = { "id":payload.query.id }
      if(yield call(verifyCodeFun,{})){
        const { success, msg, zituoTask } = yield call(queryInfo, {...params})
        detail = zituoTask 
        yield put({
          type: 'updateStore',
          payload: {detail}
        })
      }
    },
    // 获取成员
    *showUse({payload}, {put, select, call}){
      let {userList} = yield select(state => state[tmpModule.namespace])
      let params = { "id":payload.query.id }
      const { success, msg,rows } = yield call(showUse, {...params})
      userList = rows
      yield put({
        type: 'updateStore',
        payload: {userList} 
      })
    },
    //获取区域项目
    *getProjectList({payload},{put,select,call}){
      let {areaList} = yield select(state => state[tmpModule.namespace])
      let params = {
          level : 2,
        }
      const { data } = yield call(getProjectList, {...params})
        let list = data
        if(list.length){
        areaList =  list.map((item,index)=>{
          return {
            label: item.buname,
            value: item.buguid,
            children: item.childSysUninessunitList
          }
        })
        yield put({type: 'updateStore', payload: {areaList}})
      }
    },
    *verifyCodeFun({payload},{put,select,call}){
      if(yield call(verifyCodeFun,{})){
        // 执行业务操作
        yield put({type: 'queryInfo',payload: payload}) // end dispatch
        yield put({type: 'showUse',payload: payload}) // end dispatch
        yield put({type: 'getProjectList', payload: {}})
      }
    }
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
